<template>
  <a-spin :spinning="confirmLoading">
  <div class="account-settings-info-view">
    <a-row :gutter="16">
      <a-col :md="20" :lg="16">
        <a-form  :form="form">
          <a-form-item label="账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-decorator="[ 'username', {rules: [{ required: true, message: '账号' }]}]"/>
          </a-form-item>
          <a-form-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入姓名" v-decorator="[ 'realname', {rules: [{ required: true, message: '姓名' }]}]"/>
          </a-form-item>
          <a-form-item label="个人简介" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-textarea rows="4" v-decorator="['profile']" placeholder="请输入个人简介..."/>
          </a-form-item>

          <a-form-item label="生日" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-date-picker
              style="width: 100%"
              v-decorator="['birthday', {initialValue:!model.birthday?null:moment(model.birthday,dateFormat)}]"
              placeholder="请选择生日"
             />
          </a-form-item>

          <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select  placeholder="请选择性别" v-decorator="['sex']">
              <a-select-option :value="1">男</a-select-option>
              <a-select-option :value="2">女</a-select-option>
            </a-select>
          </a-form-item>

          <a-form-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入邮箱"  v-decorator="['email',validatorRules.email]"/>
          </a-form-item>

          <a-form-item label="手机号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入手机号码" v-decorator="['phone',validatorRules.phone]"/>
          </a-form-item>

          <a-form-item
            :wrapperCol="{ span: 24 }"
            style="text-align: center"
          >
            <a-button @click="handleSubmit" type="primary" :loading="confirmLoading">提交</a-button>
            <a-button style="margin-left: 8px">重置</a-button>
          </a-form-item>
        </a-form>

      </a-col>
      <a-col :md="24" :lg="8" :style="{ minHeight: '180px' }">
        <div class="ant-upload-preview" @click="$refs.modal.edit(1)">
          <a-icon type="cloud-upload-o" class="upload-icon"/>
          <div class="mask">
            <a-icon type="plus"/>
          </div>
          <img :src="option.img"/>
        </div>
      </a-col>

    </a-row>

    <avatar-modal ref="modal">

    </avatar-modal>
  </div>
  </a-spin>
</template>

<script>
  import AvatarModal from './AvatarModal'
  import { filterObj } from '@/utils/util';
  import pick from 'lodash.pick'
  import moment from 'moment'
  import Vue from 'vue'
  // 引入搜索部门弹出框的组件
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import { getAction, httpAction,putAction } from '@/api/manage'
  import {editSelf } from '@/api/api'

  export default {
    components: {
      AvatarModal
    },
    data() {
      return {
        // cropper
        url: {
          queryCurrentUser:"/sys/user/queryCurrentUser"

        },
        confirmLoading:true,
        preview: {},
        model: {},
        dateFormat:"YYYY-MM-DD",
        validatorRules:{
          username:{
            rules: [{
              required: true, message: '请输入用户账号!'
            },{
              validator: this.validateUsername,
            }]
          },
          realname:{rules: [{ required: true, message: '请输入用户名称!' }]},
          phone:{rules: [{required:true,validator: this.validatePhone}]},
          email:{rules: [{type: 'email', message: '请输入正确格式的电子邮箱!',}]},
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        form: this.$form.createForm(this),
        option: {
          img: '/avatar2.jpg',
          info: true,
          size: 1,
          outputType: 'jpeg',
          canScale: false,
          autoCrop: true,
          // 只有自动截图开启 宽度高度才生效
          autoCropWidth: 180,
          autoCropHeight: 180,
          fixedBox: true,
          // 开启宽度和高度比例
          fixed: true,
          fixedNumber: [1, 1]
        }
      }
    },
    created() {
      this.loadData();
    },
    methods: {
      moment,
      handleSubmit(){
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let avatar = that.model.avatar;
            if(!values.birthday){
              values.birthday = '';
            }else{
              values.birthday = values.birthday.format(this.dateFormat);
            }
            let formData = Object.assign(this.model, values);
            formData.avatar = avatar;
            let obj;
            obj=editSelf(formData);
            obj.then((res)=>{
              if(res.success){
                that.$message.success(res.message);
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })

          }
        })
      },
      loadData (){
        getAction(this.url.queryCurrentUser,{}).then((res)=>{
          if(res.success){
            this.model = Object.assign({}, res.result);
            this.$nextTick(() => {
              this.form.setFieldsValue(pick(this.model,'username','sex','realname','email','phone'))
            });
            this.confirmLoading=false;
          }
        })
      },
      validatePhone(rule, value, callback){
        if(!value || new RegExp(/^1[3|4|5|7|8][0-9]\d{8}$/).test(value)){
          callback();
        }else{
          callback("请输入正确格式的手机号码!");
        }
      },

    }
  }
</script>

<style lang="scss" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }

    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0, 0, 0, 0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }

  }
</style>